package org.lex.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class LineBasedProcessTest {
	private static final String PROMPT = "shell>";

	public static void main(String[] args) {
		try {
			new LineBasedProcessTest().windowsPingTest();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void windowsPingTest() throws Exception {
		// this.loopProcess( "ping", "10.85.25.200" );
		this.loopProcess("cmd");
	}

	private void loopProcess(String... command) throws Exception {
		LineBasedProcess lbp = new LineBasedProcess(command);
		lbp.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				System.out.println(e.getSource());
			}
		});
		PrintWriter writer = new PrintWriter(System.out);
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		lbp.start();
		while (true) {
			writer.print(PROMPT);
			String line = reader.readLine();
			if (null == line)
				continue;
			if (line.startsWith("exit") || line.startsWith("quit")) {
				lbp.destroy();
				return;
			}
			lbp.println(line);
		}
	}
}
